package com.groupseven.backend.services.impl;

import com.groupseven.backend.domain.User.User;
import com.groupseven.backend.domain.User.UserRole;
import com.groupseven.backend.mapper.UserMapper;
import com.groupseven.backend.services.UserService;
import com.groupseven.backend.test.DataEncode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Transactional
@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper ;
    @Autowired
    private DataEncode dataEncode;
    /**
     * @Author 王志华
     * @param user
     * @return
     */
    public User selectById(User user){
        return userMapper.selectById(user.getUserid());
    }

    /**
     * @Author 王志华
     * @return
     */
    public List<User> getAllUsers(){
        return userMapper.getAllUsers();
    }

    /**
     * @Author 王志华
     * @param username
     * @return
     */
    public User selectByUsername(String username){
        return userMapper.selectByUsername(username);
    }

    /**
     * @Author 王志华
     * @param ur
     * @return
     */
    public int updateUserRole(UserRole ur){
        return userMapper.updateUserRole(ur);
    }

    /**
     * @Author 王志华
     * @param user
     * @return
     */
    public int addUser(User user){
        return userMapper.addUser(user);
    }

    /**
     * @Author 王志华
     * @param Usernum
     * @return
     */
    public int deleteByUsername(int Usernum){
        return userMapper.deleteByUsernum(Usernum);
    }

    /**
     * @Author 王志华
     * @param date
     * @param Username
     * @return
     */
    public int updateLastLoginDate(String date,String Username){
        return userMapper.updateLastLoginDate(date,Username);
    }

    /**
     * @Author 王志华
     * @param Username
     * @return
     */
    public int logout(String Username){
        return userMapper.logout(Username);
    }

    /**
     * @Author 王志华
     * @param user
     * @return
     */
    public User selectByUsernameAndPassword(User user){
        return userMapper.selectByUsernameAndPassword(user);
    }

    /**
     * @Author 王志华
     * @param users
     * @return
     */
    public boolean multi_delete(List<User> users){
        List<String> ids = new ArrayList<>();
        for(User iterator : users){
            ids.add(iterator.getUserid());
        }
        Map<String,Object> query = new HashMap<>();
        query.put("userid",ids);
        if(userMapper.multi_delete(query)==0){
            return false;
        }
        return true;
    }

    /**
     * @Author 王志华
     * @param query
     * @return
     */
    public List<User> FuzzySearch(String query){
        List<User> tmp = userMapper.getAllUsers();
        List<User> users = new ArrayList<>();
        for(User iterator : tmp){
            iterator.setUsername(dataEncode.decodeBae64(iterator.getUsername()));
            iterator.setPassword(dataEncode.decodeBae64(iterator.getPassword()));
            iterator.setUserid(dataEncode.decodeBae64(iterator.getUserid()));
            if(iterator.getUsername().contains(query)||iterator.getUserid().contains(query)||iterator.getPassword().contains(query)){
                users.add(iterator);
            }
        }
        return users;
    }
}
